package com.xnx.bincker.leader.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.xnx.bincker.leader.core.domain.BasicEntity;
import lombok.Data;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

/**
 * 菜单
 */
@Entity
@Data
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer","fieldHandler"})
public class Menu extends BasicEntity {
    private static final long serialVersionUID = 8423490614096069577L;
    @Id
    private String id;
	/**
	 * 名称
	 * 如果是父级菜单，可分为组名称和父级菜单本身名称，用“:”隔开, 如“系统管理:首页”
	 */
	private String name;

	/**
	 * 地址
	 */
	@Column(unique = true)
	private String url;

	/**
	 * 父级
	 */
	@JsonIgnore
	@ManyToOne(fetch = FetchType.LAZY)
	private Menu parent;

	/**
	 * 子集
	 */
	@OneToMany(mappedBy = "parent", cascade = {CascadeType.REMOVE})
	private List<Menu> children;

	/**
	 * 页面下的请求权限
	 * 用于对权限的分组规划
	 */
	@JsonIgnore
	@OneToMany(mappedBy = "parentMenu")
	private List<Authority> authorities;

	/**
	 * 菜单本身的权限
	 */
	@JsonIgnore
	@OneToOne(cascade = {CascadeType.REMOVE, CascadeType.MERGE})
	private Authority authority;

	/**
	 * 是否显示
	 */
	@JsonIgnore
	@Column(columnDefinition = "bit(1) not null default true")
	private Boolean display;

	/**
	 * 排序
	 */
	@SuppressWarnings("JpaDataSourceORMInspection")
    @Column(name = "[order]")
	private Integer order;

	@Override
	public String toString() {
		return "Menu{" +
				"name='" + name + '\'' +
				", url='" + url + '\'' +
				'}';
	}

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Menu)) return false;
        if (!super.equals(o)) return false;
        Menu menu = (Menu) o;
        return Objects.equals(id, menu.getId()) &&
                Objects.equals(name, menu.getName()) &&
                Objects.equals(url, menu.getUrl()) &&
                Objects.equals(parent, menu.getParent()) &&
                Objects.equals(display, menu.getDisplay()) &&
                Objects.equals(order, menu.getOrder());
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), id, name, url, parent, display, order);
    }
}
